package top.lilele.adminSystem.service;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.lilele.adminSystem.dto.AddUserDepartmentDto;
import top.lilele.adminSystem.dto.DeptUserListDto;
import top.lilele.adminSystem.dto.GetUserByDeptIdDto;
import top.lilele.adminSystem.dto.IdParams;
import top.lilele.adminSystem.entity.AdminDepartmentEntity;
import top.lilele.adminSystem.entity.AdminUserDepartmentEntity;
import top.lilele.adminSystem.exception.business.DepartmentHasChildrenException;
import top.lilele.adminSystem.mapper.AdminDepartmentMapper;
import top.lilele.adminSystem.mapper.AdminUserDepartmentMapper;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lilele
 */
@Service
public class AdminDepartmentService {
    @Resource
    AdminDepartmentMapper adminDepartmentMapper;

    @Resource
    AdminUserDepartmentMapper adminUserDepartmentMapper;


    public String add(AdminDepartmentEntity adminDepartmentEntity) {
        adminDepartmentMapper.insert(adminDepartmentEntity);
        return adminDepartmentEntity.getId();
    }

    public String updateDepartmentById(AdminDepartmentEntity adminDepartmentEntity) {
        adminDepartmentMapper.updateById(adminDepartmentEntity);
        return adminDepartmentEntity.getId();
    }

    public List<Tree<String>> selectAllDepartment() {
        List<AdminDepartmentEntity> list = adminDepartmentMapper.selectList(null);
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setIdKey("id");
        treeNodeConfig.setParentIdKey("parentId");
        treeNodeConfig.setChildrenKey("children");
        treeNodeConfig.setNameKey("departmentName");
        treeNodeConfig.setWeightKey("sort");
        List<Tree<String>> treeNodes = TreeUtil.build(list, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId());
                    tree.setParentId(treeNode.getParentId());
                    tree.setName(treeNode.getDepartmentName());
                    tree.setWeight(treeNode.getSort());
                });
        return treeNodes;
    }

    /**
     * 根据部门id查询当前部门的用户列表
     *
     * @param getUserByDeptIdDto
     * @return
     */
    public List<DeptUserListDto> selectUserListByDeptId(GetUserByDeptIdDto getUserByDeptIdDto) {
        List<DeptUserListDto> list = adminDepartmentMapper.selectUserListByDeptId(getUserByDeptIdDto);
        return list;
    }

    /**
     * 根据id删除部门
     *
     * @param idParams
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteDepartmentById(IdParams idParams) throws DepartmentHasChildrenException {
        List<AdminDepartmentEntity> list = adminDepartmentMapper.selectList(new LambdaQueryWrapper<AdminDepartmentEntity>().eq(AdminDepartmentEntity::getParentId, idParams.getId()));
        if (list.size() != 0) {
            throw new DepartmentHasChildrenException("该部门下还有子部门，请先删除子部门。");
        }
        // 删除部门表
        adminDepartmentMapper.deleteById(idParams.getId());
        // 删除部门-用户关联表
        adminUserDepartmentMapper.delete(new LambdaQueryWrapper<AdminUserDepartmentEntity>().eq(AdminUserDepartmentEntity::getDepartmentId, idParams.getId()));
    }

    /**
     * @param addUserDepartmentDto
     */
    public void addUserDepartment(AddUserDepartmentDto addUserDepartmentDto) {
        AdminUserDepartmentEntity adminUserDepartmentEntity = new AdminUserDepartmentEntity();
        addUserDepartmentDto.getIds().forEach(id -> {
            adminUserDepartmentEntity.setUserId(id);
            adminUserDepartmentEntity.setDepartmentId(addUserDepartmentDto.getDeptId());
            adminUserDepartmentMapper.insert(adminUserDepartmentEntity);
        });
    }

    /**
     * 删除用户-部门关联关系
     *
     * @param adminUserDepartmentEntity
     */
    public void deleteUserDepartment(AdminUserDepartmentEntity adminUserDepartmentEntity) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userId", adminUserDepartmentEntity.getUserId());
        queryWrapper.eq("departmentId", adminUserDepartmentEntity.getDepartmentId());
        adminUserDepartmentMapper.delete(queryWrapper);
    }
}
